<template>
  <div>
    <el-form :inline="true">
      <el-form-item label="站点名称" prop="stationId" label-width="140px">
        <el-select v-model="searchForm.stationId" class="filter-item" placeholder="Please select station" filterable>
          <el-option v-for="item in stationOption" :key="item.key" :label="item.value" :value="item.key" />
        </el-select>
      </el-form-item>
      <el-form-item label="包裹尺寸" prop="size" label-width="140px">
        <el-select v-model="searchForm.size" clearable class="filter-item" placeholder="Please select size">
          <el-option v-for="item in sizeOption" :key="item.key" :label="item.value" :value="item.key" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getCollectList">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-divider><h1>包裹待揽收列表</h1></el-divider>
    <br>
    <div v-if="this.tableData.length===0">
      <el-empty description="没有更多包裹信息咯"></el-empty>
    </div>
    <div v-else>
      <el-table
          :data="tableData"
          tooltip-effect="dark"
          style="width: 200%"
          border
          stripe>

        <el-table-column
            prop="orderId"
            label="订单编号"
            width="160">
        </el-table-column>
        <el-table-column
            prop="stationInfo.address"
            label="站点名称"
            width="220">
        </el-table-column>
        <el-table-column
            prop="sendOrder.posName"
            label="委托人"
            width="80">
        </el-table-column>
        <el-table-column
            prop="sendOrder.posPhone"
            label="联系电话"
            width="120">
        </el-table-column>
        <el-table-column
            prop="sendOrder.porAddress"
            label="目的地"
            width="220">
        </el-table-column>
        <el-table-column
            prop="enterDate"
            label="入库日期"
            width="160">
          <template slot-scope="scope">
            <el-text>{{scope.row.entertDate | dateFilter}}</el-text>
          </template>
        </el-table-column>

        <el-table-column
            prop="sendOrder.size"
            label="包裹规格"
            width="80"
        >
          <template slot-scope="scope">
            <el-tag size="small" v-if="scope.row.sendOrder.size===0" type="success">小件</el-tag>
            <el-tag size="small" v-if="scope.row.sendOrder.size===1" type="success">中件</el-tag>
            <el-tag size="small" v-if="scope.row.sendOrder.size===2" type="success">大件</el-tag>
            <el-tag size="small" v-if="scope.row.sendOrder.size===3" type="success">超大件</el-tag>
          </template>
        </el-table-column>
        <el-table-column
            prop="icon"
            label="操作">

          <template slot-scope="scope" v-if="scope.row.status===0">
            <router-link :to="{
                path: '/mew/orderdetail',
                query: {
                  orderId: scope.row.orderId
                }
              }">
              <el-button type="text">详情</el-button>
            </router-link>
            <el-divider direction="vertical"></el-divider>
            <el-button type="text" @click="collectExpress(scope.row.id)">揽件</el-button>
          </template>
        </el-table-column>

      </el-table>

      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          layout="total, sizes, prev, pager, next, jumper"
          :page-sizes="[10, 20, 50, 100]"
          :current-page="current"
          :page-size="size"
          :total="total">
      </el-pagination>
    </div>

    <!--新增对话框-->
    <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="600px"
        :before-close="handleClose">

      <el-form :model="editForm" :rules="editFormRules" ref="editForm">
        <el-form-item label="当前站点" prop="thisSite" label-width="100px">
          <el-input v-model="editForm.thisSite" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="下一站" prop="targetSite" label-width="100px">
          <el-select v-model="editForm.targetSite" class="filter-item" placeholder="Please select station" filterable>
            <el-option v-for="item in stationOption" :key="item.key" :label="item.value" :value="item.key" />
          </el-select>
        </el-form-item>
        <el-form-item label="最终站" prop="destination" label-width="100px">
          <el-switch v-model="editForm.destination" :active-value="true" :inactive-value="false" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('editForm')">取 消</el-button>
        <el-button type="primary" @click="submitForm('editForm')">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {getExpressInfo, getExpressList, getStationList, saveLogistics} from "@/api/dm";

export default {
  name: "Collect",
  data() {
    return {
      tableData: [],
      searchForm: {},
      sizeOption: [
        { key: 0, value: '小件'},
        { key: 1, value: '中件'},
        { key: 2, value: '大件'},
        { key: 3, value: '超大件'},
      ],
      stationOption: [],
      total: 0,
      size: 10,
      current: 1,
      dialogVisible: false,
      editForm: {
        thisSite: '',
        targetSite: '',
        destination: null
      },
      editFormRules: {
        targetSite: [
          {required: true, message: '请选择站点信息', trigger: 'blur'}
        ]
      }
    }
  },
  created() {
    this.getStationList()
    this.getCollectList()
  },
  methods: {
    getStationList(){
      getStationList().then(res =>{
        res.data.data.forEach(stationItem =>{
          this.stationOption.push({ key: stationItem.address, value: stationItem.address})
        })
      })
    },
    getCollectList(){
      getExpressList(this.searchForm.stationId, this.searchForm.size).then(res =>{
        this.tableData = res.data.data.records
        this.size = res.data.data.size
        this.current = res.data.data.current
        this.total = res.data.data.total
      })
    },
    handleSizeChange(val) {
      this.size = val
      this.getCollectList()
    },
    handleCurrentChange(val) {
      this.current = val
      this.getCollectList()
    },
    collectExpress(id){
      getExpressInfo(id).then(res =>{
        this.editForm.id = id
        this.editForm.orderId = res.data.data.orderId
        this.editForm.thisSite = res.data.data.stationInfo.address
        this.dialogVisible = true
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dialogVisible = false
      this.editForm = {}
    },
    handleClose() {
      this.resetForm('editForm')
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          saveLogistics(this.editForm).then(res =>{
            this.$message({
              showClose: true,
              message: '恭喜你，操作成功',
              type: 'success',
              onClose:() => {
                this.getCollectList()
              }
            });
            this.dialogVisible = false
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    }
  }
}
</script>

<style scoped>
.el-pagination {
  float: right;
  margin-top: 22px;
}
</style>